﻿unit SmOrderMstModel;

interface

uses
  FastMM4,
  FastMove,
  System.SysUtils,
  System.Classes,
  System.Types,
  Winapi.Windows,
  Data.DB,
  Data.Win.ADODB,
  Datasnap.DBClient,
  Datasnap.Provider,
  {$IFDEF UNICODE}
  AnsiStrings,
  {$ENDIF }
  QBParcel,
  DllSpread,
  Generics.Collections,
  DateUtils,
  QJson,
  UserConnection,
  DBAccessor,
  DllPlugin,
  StrUtils,
  SK0001Comm;

type

  TSmOrderMstModel = class(TObject)
  public 
    const cModuleKey='SmOrderMst'; 
    const cTblName ='sm_order_mst';
    const cBaseView = 'view_sm_order_mst';
    const cObjNameKey='ObjName';
    const cObjGUIDKey='ObjGUID';
    const cObjItemsKey='Items';
    const cCdsModelKey='CdsModel';
    
    class function GetCountSQL(const iCondition:string; const dLoadMode:eDBLoadMode=eDBLoadMode.dblmTable):string;
    class function GetQuerySQL(const iCondition:string; const dOrderBy:string=''; const dLoadMode:eDBLoadMode=eDBLoadMode.dblmTable):string;
    class function GetPageQuerySQL(const iPageNo,iPageSize:Integer; const iCondition,iOrderBy:string; const dLoadMode:eDBLoadMode=eDBLoadMode.dblmTable):string;

    const cOrderIdKey='order_id';
    const cOrderBillTypeKey='order_bill_type';
    const cCustomerIdKey='customer_id';
    const cOrderStatusKey='order_status';
    const cOrderRemarkKey='order_remark';
    const cInvoiceTypeIdKey='invoice_type_id';
    const cPaywayIdKey='payway_id';
    const cShipTypeIdKey='ship_type_id';
    const cOrgIdKey='org_id';
    const cEmpIdKey='emp_id';
    const cDeptIdKey='dept_id';
    const cLinkmanIdKey='linkman_id';
    const cOrderAmountKey='order_amount';
    const cOrderSendStatusKey='order_send_status';
    const cOrderSettlementStatusKey='order_settlement_status';
    const cRecordPsnKey='record_psn';
    const cRecordDateKey='record_date';
    const cModifyPsnKey='modify_psn';
    const cModifyDateKey='modify_date';
    const cApprovePsnKey='approve_psn';
    const cApproveDateKey='approve_date';
    const cCancelPsnKey='cancel_psn';
    const cCancelDateKey='cancel_date';
    const cTimeStampKey='time_stamp';
  end;
  
  TSmOrderMst = class(TObject)
  private    
    fOrderId:string;
    fOrderBillType:Integer;
    fCustomerId:string;
    fOrderStatus:Integer;
    fOrderRemark:string;
    fInvoiceTypeId:string;
    fPaywayId:string;
    fShipTypeId:string;
    fOrgId:string;
    fEmpId:string;
    fDeptId:string;
    fLinkmanId:string;
    fOrderAmount:Real;
    fOrderSendStatus:Integer;
    fOrderSettlementStatus:Integer;
    fRecordPsn:string;
    fRecordDate:TDateTime;
    fModifyPsn:string;
    fModifyDate:TDateTime;
    fApprovePsn:string;
    fApproveDate:TDateTime;
    fCancelPsn:string;
    fCancelDate:TDateTime;
    fTimeStamp:Int64;
    
  protected
    fObjGUID:string;
    fInitDate:TDateTime;
    fCdsModel:TClientDataSet;
    function ObjectToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    
  public
    constructor Create();overload;
    destructor Destroy;override;
    
    procedure ResetObject;
    
    function LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
    function SaveToStr():string;
    
    property ObjGUID:string read fObjGUID;
    property OrderId:string read fOrderId write fOrderId;
    property OrderBillType:Integer read fOrderBillType write fOrderBillType;
    property CustomerId:string read fCustomerId write fCustomerId;
    property OrderStatus:Integer read fOrderStatus write fOrderStatus;
    property OrderRemark:string read fOrderRemark write fOrderRemark;
    property InvoiceTypeId:string read fInvoiceTypeId write fInvoiceTypeId;
    property PaywayId:string read fPaywayId write fPaywayId;
    property ShipTypeId:string read fShipTypeId write fShipTypeId;
    property OrgId:string read fOrgId write fOrgId;
    property EmpId:string read fEmpId write fEmpId;
    property DeptId:string read fDeptId write fDeptId;
    property LinkmanId:string read fLinkmanId write fLinkmanId;
    property OrderAmount:Real read fOrderAmount write fOrderAmount;
    property OrderSendStatus:Integer read fOrderSendStatus write fOrderSendStatus;
    property OrderSettlementStatus:Integer read fOrderSettlementStatus write fOrderSettlementStatus;
    property RecordPsn:string read fRecordPsn write fRecordPsn;
    property RecordDate:TDateTime read fRecordDate write fRecordDate;
    property ModifyPsn:string read fModifyPsn write fModifyPsn;
    property ModifyDate:TDateTime read fModifyDate write fModifyDate;
    property ApprovePsn:string read fApprovePsn write fApprovePsn;
    property ApproveDate:TDateTime read fApproveDate write fApproveDate;
    property CancelPsn:string read fCancelPsn write fCancelPsn;
    property CancelDate:TDateTime read fCancelDate write fCancelDate;
    property TimeStamp:Int64 read fTimeStamp write fTimeStamp;
  end;

  TSmOrderMstList = class(TDictionary<string,TSmOrderMst>)
  private
    fCdsModel:TClientDataSet;
    fSortKeys:TStrings;
    
  public
    constructor Create();overload;
    destructor Destroy;override;
    
    procedure ResetObject;
    
    function LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
    function SaveToStr():string;
    
    function FindByKey(const iKey:string):TSmOrderMst;
    function IsExist(iKey:string):Boolean;
    function Add(const iItem:TSmOrderMst):string;
    function Delete(const iKey:string):Boolean;
    function Remove(const iKey:string):TSmOrderMst;
    procedure Clear;
    
    property SortKeys:TStrings read fSortKeys write fSortKeys;
  end;

implementation

class function TSmOrderMstModel.GetCountSQL(const iCondition:string; const dLoadMode:eDBLoadMode):string;
var
  ReadName:string;
begin
  case dLoadMode of
    dblmNone: ReadName:=TSmOrderMstModel.cTblName;
    dblmTable: ReadName:=TSmOrderMstModel.cTblName;
    dblmView: ReadName:=TSmOrderMstModel.cBaseView;
  end;
  
  if SameStr(Trim(iCondition),EmptyStr) then
  begin
    Result:=Format(string('SELECT COUNT(*) AS ''COUNT'' FROM %s;'),[ReadName]);
  end else begin
    Result:=Format(string('SELECT COUNT(*) AS ''COUNT'' FROM %s WHERE %s;'),[ReadName,iCondition]);
  end;
end;

class function TSmOrderMstModel.GetQuerySQL(const iCondition:string; const dOrderBy:string; const dLoadMode:eDBLoadMode):string;
var
  ReadName:string;
begin
  case dLoadMode of
    dblmNone: ReadName:=TSmOrderMstModel.cTblName;
    dblmTable: ReadName:=TSmOrderMstModel.cTblName;
    dblmView: ReadName:=TSmOrderMstModel.cBaseView;
  end;
  
  if SameStr(Trim(iCondition),EmptyStr) then
  begin
    Result:=Format(string('SELECT * FROM %s %s;'),[ReadName,
                                                 IfThen(SameStr(dOrderBy,EmptyStr),'','ORDER BY '+dOrderBy)]);
  end else begin
    Result:=Format(string('SELECT * FROM %s WHERE %s %s;'),[ReadName,iCondition,
                                                            IfThen(SameStr(dOrderBy,EmptyStr),'','ORDER BY '+dOrderBy)]);
  end;
end;

class function TSmOrderMstModel.GetPageQuerySQL(const iPageNo,iPageSize:Integer; const iCondition,iOrderBy:string; const dLoadMode:eDBLoadMode):string;
var
  ReadName:string;
begin
  case dLoadMode of
    dblmNone: ReadName:=TSmOrderMstModel.cTblName;
    dblmTable: ReadName:=TSmOrderMstModel.cTblName;
    dblmView: ReadName:=TSmOrderMstModel.cBaseView;
  end;
  
  if SameStr(Trim(iCondition),EmptyStr) then
  begin      
    Result:=Format(string('SELECT * FROM %s'),[ReadName]);
  end else begin
    Result:=Format(string('SELECT * FROM %s WHERE %s'),[ReadName,iCondition]);
  end;
  Result:=Format(string('SELECT TOP %d * FROM (SELECT row_number() OVER(ORDER BY %s) AS _row_no_,* FROM (%s) AS A) AS B WHERE _row_no_>%d ORDER BY _row_no_'),
                  [iPageSize,
                  IfThen(SameStr(Trim(iOrderBy),EmptyStr),'',iOrderBy),
                  Result,
                  (iPageNo-1)*iPageSize]);
end;

constructor TSmOrderMst.Create();
begin
  inherited Create;
  fInitDate:=EncodeDateTime( 1899, 12, 30, 0, 0, 0, 0 );
  fCdsModel:=TClientDataSet.Create(nil);
  ResetObject;
end;

destructor TSmOrderMst.Destroy;
begin
  FreeAndNil(fCdsModel);
  inherited Destroy;
end;

procedure TSmOrderMst.ResetObject;
begin
  fObjGUID:=TSK0001Comm.GetGUID;
  fOrderId:=EmptyStr;
  fOrderBillType:=-1;
  fCustomerId:=EmptyStr;
  fOrderStatus:=-1;
  fOrderRemark:=EmptyStr;
  fInvoiceTypeId:=EmptyStr;
  fPaywayId:=EmptyStr;
  fShipTypeId:=EmptyStr;
  fOrgId:=EmptyStr;
  fEmpId:=EmptyStr;
  fDeptId:=EmptyStr;
  fLinkmanId:=EmptyStr;
  fOrderAmount:=-1;
  fOrderSendStatus:=-1;
  fOrderSettlementStatus:=-1;
  fRecordPsn:=EmptyStr;
  fRecordDate:=fInitDate;
  fModifyPsn:=EmptyStr;
  fModifyDate:=fInitDate;
  fApprovePsn:=EmptyStr;
  fApproveDate:=fInitDate;
  fCancelPsn:=EmptyStr;
  fCancelDate:=fInitDate;
  fTimeStamp:=-1;
end;

function TSmOrderMst.LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
begin
  if (iCds<>nil) then
  begin
    ResetObject;
    if (iCds.FindField(TSmOrderMstModel.cOrderIdKey)<>nil) then
    begin
      fOrderId:=iCds.FieldByName(TSmOrderMstModel.cOrderIdKey).AsString;     
      fObjGUID:=TSK0001Comm.SetObjGUID(fOrderId);
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cOrderIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cOrderBillTypeKey)<>nil) then
    begin
      fOrderBillType:=iCds.FieldByName(TSmOrderMstModel.cOrderBillTypeKey).AsInteger;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cOrderBillTypeKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cCustomerIdKey)<>nil) then
    begin
      fCustomerId:=iCds.FieldByName(TSmOrderMstModel.cCustomerIdKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cCustomerIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cOrderStatusKey)<>nil) then
    begin
      fOrderStatus:=iCds.FieldByName(TSmOrderMstModel.cOrderStatusKey).AsInteger;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cOrderStatusKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cOrderRemarkKey)<>nil) then
    begin
      fOrderRemark:=iCds.FieldByName(TSmOrderMstModel.cOrderRemarkKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cOrderRemarkKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cInvoiceTypeIdKey)<>nil) then
    begin
      fInvoiceTypeId:=iCds.FieldByName(TSmOrderMstModel.cInvoiceTypeIdKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cInvoiceTypeIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cPaywayIdKey)<>nil) then
    begin
      fPaywayId:=iCds.FieldByName(TSmOrderMstModel.cPaywayIdKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cPaywayIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cShipTypeIdKey)<>nil) then
    begin
      fShipTypeId:=iCds.FieldByName(TSmOrderMstModel.cShipTypeIdKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cShipTypeIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cOrgIdKey)<>nil) then
    begin
      fOrgId:=iCds.FieldByName(TSmOrderMstModel.cOrgIdKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cOrgIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cEmpIdKey)<>nil) then
    begin
      fEmpId:=iCds.FieldByName(TSmOrderMstModel.cEmpIdKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cEmpIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cDeptIdKey)<>nil) then
    begin
      fDeptId:=iCds.FieldByName(TSmOrderMstModel.cDeptIdKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cDeptIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cLinkmanIdKey)<>nil) then
    begin
      fLinkmanId:=iCds.FieldByName(TSmOrderMstModel.cLinkmanIdKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cLinkmanIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cOrderAmountKey)<>nil) then
    begin
      fOrderAmount:=iCds.FieldByName(TSmOrderMstModel.cOrderAmountKey).AsFloat;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cOrderAmountKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cOrderSendStatusKey)<>nil) then
    begin
      fOrderSendStatus:=iCds.FieldByName(TSmOrderMstModel.cOrderSendStatusKey).AsInteger;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cOrderSendStatusKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cOrderSettlementStatusKey)<>nil) then
    begin
      fOrderSettlementStatus:=iCds.FieldByName(TSmOrderMstModel.cOrderSettlementStatusKey).AsInteger;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cOrderSettlementStatusKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cRecordPsnKey)<>nil) then
    begin
      fRecordPsn:=iCds.FieldByName(TSmOrderMstModel.cRecordPsnKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cRecordPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cRecordDateKey)<>nil) then
    begin
      if iCds.FindField(TSmOrderMstModel.cRecordDateKey).IsNull then
      begin
        fRecordDate:=fInitDate;
      end else begin
        fRecordDate:=iCds.FieldByName(TSmOrderMstModel.cRecordDateKey).AsDateTime;
      end;    
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cRecordDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cModifyPsnKey)<>nil) then
    begin
      fModifyPsn:=iCds.FieldByName(TSmOrderMstModel.cModifyPsnKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cModifyPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cModifyDateKey)<>nil) then
    begin
      if iCds.FindField(TSmOrderMstModel.cModifyDateKey).IsNull then
      begin
        fModifyDate:=fInitDate;
      end else begin
        fModifyDate:=iCds.FieldByName(TSmOrderMstModel.cModifyDateKey).AsDateTime;
      end;    
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cModifyDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cApprovePsnKey)<>nil) then
    begin
      fApprovePsn:=iCds.FieldByName(TSmOrderMstModel.cApprovePsnKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cApprovePsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cApproveDateKey)<>nil) then
    begin
      if iCds.FindField(TSmOrderMstModel.cApproveDateKey).IsNull then
      begin
        fApproveDate:=fInitDate;
      end else begin
        fApproveDate:=iCds.FieldByName(TSmOrderMstModel.cApproveDateKey).AsDateTime;
      end;    
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cApproveDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cCancelPsnKey)<>nil) then
    begin
      fCancelPsn:=iCds.FieldByName(TSmOrderMstModel.cCancelPsnKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cCancelPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cCancelDateKey)<>nil) then
    begin
      if iCds.FindField(TSmOrderMstModel.cCancelDateKey).IsNull then
      begin
        fCancelDate:=fInitDate;
      end else begin
        fCancelDate:=iCds.FieldByName(TSmOrderMstModel.cCancelDateKey).AsDateTime;
      end;    
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cCancelDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cTimeStampKey)<>nil) then
    begin
      fTimeStamp:=iCds.FieldByName(TSmOrderMstModel.cTimeStampKey).AsLargeInt;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cTimeStampKey]);
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.LoadFromCDS函数参数错误!错误内容:传入的Cds对象为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSmOrderMst.ObjectToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
begin
  if (iCds<>nil) then
  begin
    if (iCds.FindField(TSmOrderMstModel.cOrderIdKey)<>nil) then
    begin
      iCds.FieldByName(TSmOrderMstModel.cOrderIdKey).AsString:=fOrderId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cOrderIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cOrderBillTypeKey)<>nil) then
    begin
      iCds.FieldByName(TSmOrderMstModel.cOrderBillTypeKey).AsInteger:=fOrderBillType;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cOrderBillTypeKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cCustomerIdKey)<>nil) then
    begin
      iCds.FieldByName(TSmOrderMstModel.cCustomerIdKey).AsString:=fCustomerId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cCustomerIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cOrderStatusKey)<>nil) then
    begin
      iCds.FieldByName(TSmOrderMstModel.cOrderStatusKey).AsInteger:=fOrderStatus;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cOrderStatusKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cOrderRemarkKey)<>nil) then
    begin
      iCds.FieldByName(TSmOrderMstModel.cOrderRemarkKey).AsString:=fOrderRemark;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cOrderRemarkKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cInvoiceTypeIdKey)<>nil) then
    begin
      iCds.FieldByName(TSmOrderMstModel.cInvoiceTypeIdKey).AsString:=fInvoiceTypeId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cInvoiceTypeIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cPaywayIdKey)<>nil) then
    begin
      iCds.FieldByName(TSmOrderMstModel.cPaywayIdKey).AsString:=fPaywayId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cPaywayIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cShipTypeIdKey)<>nil) then
    begin
      iCds.FieldByName(TSmOrderMstModel.cShipTypeIdKey).AsString:=fShipTypeId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cShipTypeIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cOrgIdKey)<>nil) then
    begin
      iCds.FieldByName(TSmOrderMstModel.cOrgIdKey).AsString:=fOrgId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cOrgIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cEmpIdKey)<>nil) then
    begin
      iCds.FieldByName(TSmOrderMstModel.cEmpIdKey).AsString:=fEmpId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cEmpIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cDeptIdKey)<>nil) then
    begin
      iCds.FieldByName(TSmOrderMstModel.cDeptIdKey).AsString:=fDeptId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cDeptIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cLinkmanIdKey)<>nil) then
    begin
      iCds.FieldByName(TSmOrderMstModel.cLinkmanIdKey).AsString:=fLinkmanId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cLinkmanIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cOrderAmountKey)<>nil) then
    begin
      iCds.FieldByName(TSmOrderMstModel.cOrderAmountKey).AsFloat:=fOrderAmount;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cOrderAmountKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cOrderSendStatusKey)<>nil) then
    begin
      iCds.FieldByName(TSmOrderMstModel.cOrderSendStatusKey).AsInteger:=fOrderSendStatus;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cOrderSendStatusKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cOrderSettlementStatusKey)<>nil) then
    begin
      iCds.FieldByName(TSmOrderMstModel.cOrderSettlementStatusKey).AsInteger:=fOrderSettlementStatus;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cOrderSettlementStatusKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cRecordPsnKey)<>nil) then
    begin
      iCds.FieldByName(TSmOrderMstModel.cRecordPsnKey).AsString:=fRecordPsn;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cRecordPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cRecordDateKey)<>nil) then
    begin
      if SameDateTime(fRecordDate,fInitDate) then
      begin
        iCds.FieldByName(TSmOrderMstModel.cRecordDateKey).AsString:=EmptyStr;
      end else begin
        iCds.FieldByName(TSmOrderMstModel.cRecordDateKey).AsDateTime:=fRecordDate;
      end;    
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cRecordDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cModifyPsnKey)<>nil) then
    begin
      iCds.FieldByName(TSmOrderMstModel.cModifyPsnKey).AsString:=fModifyPsn;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cModifyPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cModifyDateKey)<>nil) then
    begin
      if SameDateTime(fModifyDate,fInitDate) then
      begin
        iCds.FieldByName(TSmOrderMstModel.cModifyDateKey).AsString:=EmptyStr;
      end else begin
        iCds.FieldByName(TSmOrderMstModel.cModifyDateKey).AsDateTime:=fModifyDate;
      end;    
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cModifyDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cApprovePsnKey)<>nil) then
    begin
      iCds.FieldByName(TSmOrderMstModel.cApprovePsnKey).AsString:=fApprovePsn;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cApprovePsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cApproveDateKey)<>nil) then
    begin
      if SameDateTime(fApproveDate,fInitDate) then
      begin
        iCds.FieldByName(TSmOrderMstModel.cApproveDateKey).AsString:=EmptyStr;
      end else begin
        iCds.FieldByName(TSmOrderMstModel.cApproveDateKey).AsDateTime:=fApproveDate;
      end;    
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cApproveDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cCancelPsnKey)<>nil) then
    begin
      iCds.FieldByName(TSmOrderMstModel.cCancelPsnKey).AsString:=fCancelPsn;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cCancelPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cCancelDateKey)<>nil) then
    begin
      if SameDateTime(fCancelDate,fInitDate) then
      begin
        iCds.FieldByName(TSmOrderMstModel.cCancelDateKey).AsString:=EmptyStr;
      end else begin
        iCds.FieldByName(TSmOrderMstModel.cCancelDateKey).AsDateTime:=fCancelDate;
      end;    
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cCancelDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmOrderMstModel.cTimeStampKey)<>nil) then
    begin
      iCds.FieldByName(TSmOrderMstModel.cTimeStampKey).AsLargeInt:=fTimeStamp;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmOrderMstModel.cTimeStampKey]);
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.ObjectToCDS函数参数错误!错误内容:传入的Cds对象为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSmOrderMst.SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
begin
  if (iCds<>nil) then
  begin
    iCds.Append;
    if ObjectToCDS(iCds,oError) then
    begin
      iCds.Post;
    end else begin
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.SaveToCDS函数参数错误!错误内容:传入的Cds对象为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSmOrderMst.LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
var
  Json:TQJson;
  MemStream:TMemoryStream;
begin
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create;
  try
    if Json.TryParse(iJsonStr) then
    begin
      if (Json.ItemByName(TSmOrderMstModel.cObjNameKey)<>nil) then
      begin
        if (Self.ClassName<>Json.ItemByName(TSmOrderMstModel.cObjNameKey).AsString) then
        begin
          //oError:=Format(string('调用%s.LoadFromStr函数！错误内容:对象类名不匹配.'),[Self.ClassName]);
          //Result:=False;
          //Exit;
        end;
      end else begin
        oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性节点.'),[Self.ClassName,TSmOrderMstModel.cObjNameKey]);
        Result:=False;
        Exit;
      end;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:解析Json失败.iJsonStr:%s'),[Self.ClassName,iJsonStr]);
      Result:=False;
      Exit;
    end;
    ResetObject;
      
    if (Json.ItemByName(TSmOrderMstModel.cCdsModelKey)<>nil) and 
      not SameStr(Trim(Json.ItemByName(TSmOrderMstModel.cCdsModelKey).AsString),EmptyStr) then
    begin
      TSK0001Comm.Base64StrToMemStream(AnsiString(Json.ItemByName(TSmOrderMstModel.cCdsModelKey).AsString),MemStream);
      if MemStream.Size>0 then
      begin
        fCdsModel.LoadFromStream(MemStream);
      end;
    end;
    
    if (Json.ItemByName(TSmOrderMstModel.cObjGUIDKey)<>nil) and 
      not SameStr(Trim(Json.ItemByName(TSmOrderMstModel.cObjGUIDKey).AsString),EmptyStr) then
    begin
      fObjGUID:=Json.ItemByName(TSmOrderMstModel.cObjGUIDKey).AsString;
    end;
    
    if Json.ItemByName('OrderId')<>nil then
    begin
      fOrderId:=Json.ItemByName('OrderId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'OrderId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('OrderBillType')<>nil then
    begin
      fOrderBillType:=Json.ItemByName('OrderBillType').AsInteger;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'OrderBillType']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('CustomerId')<>nil then
    begin
      fCustomerId:=Json.ItemByName('CustomerId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'CustomerId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('OrderStatus')<>nil then
    begin
      fOrderStatus:=Json.ItemByName('OrderStatus').AsInteger;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'OrderStatus']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('OrderRemark')<>nil then
    begin
      fOrderRemark:=Json.ItemByName('OrderRemark').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'OrderRemark']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('InvoiceTypeId')<>nil then
    begin
      fInvoiceTypeId:=Json.ItemByName('InvoiceTypeId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'InvoiceTypeId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('PaywayId')<>nil then
    begin
      fPaywayId:=Json.ItemByName('PaywayId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'PaywayId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('ShipTypeId')<>nil then
    begin
      fShipTypeId:=Json.ItemByName('ShipTypeId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ShipTypeId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('OrgId')<>nil then
    begin
      fOrgId:=Json.ItemByName('OrgId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'OrgId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('EmpId')<>nil then
    begin
      fEmpId:=Json.ItemByName('EmpId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'EmpId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('DeptId')<>nil then
    begin
      fDeptId:=Json.ItemByName('DeptId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'DeptId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('LinkmanId')<>nil then
    begin
      fLinkmanId:=Json.ItemByName('LinkmanId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'LinkmanId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('OrderAmount')<>nil then
    begin
      fOrderAmount:=Json.ItemByName('OrderAmount').AsFloat;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'OrderAmount']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('OrderSendStatus')<>nil then
    begin
      fOrderSendStatus:=Json.ItemByName('OrderSendStatus').AsInteger;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'OrderSendStatus']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('OrderSettlementStatus')<>nil then
    begin
      fOrderSettlementStatus:=Json.ItemByName('OrderSettlementStatus').AsInteger;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'OrderSettlementStatus']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RecordPsn')<>nil then
    begin
      fRecordPsn:=Json.ItemByName('RecordPsn').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RecordPsn']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RecordDate')<>nil then
    begin
      fRecordDate:=Json.ItemByName('RecordDate').AsDateTime;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RecordDate']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('ModifyPsn')<>nil then
    begin
      fModifyPsn:=Json.ItemByName('ModifyPsn').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ModifyPsn']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('ModifyDate')<>nil then
    begin
      fModifyDate:=Json.ItemByName('ModifyDate').AsDateTime;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ModifyDate']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('ApprovePsn')<>nil then
    begin
      fApprovePsn:=Json.ItemByName('ApprovePsn').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ApprovePsn']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('ApproveDate')<>nil then
    begin
      fApproveDate:=Json.ItemByName('ApproveDate').AsDateTime;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ApproveDate']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('CancelPsn')<>nil then
    begin
      fCancelPsn:=Json.ItemByName('CancelPsn').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'CancelPsn']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('CancelDate')<>nil then
    begin
      fCancelDate:=Json.ItemByName('CancelDate').AsDateTime;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'CancelDate']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('TimeStamp')<>nil then
    begin
      fTimeStamp:=Json.ItemByName('TimeStamp').AsInt64;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'TimeStamp']);
      Result:=False;
      Exit;  
    end;
    
  finally
    Json.Free;
    MemStream.Free;
  end;
  Result:=True;
end;

function TSmOrderMst.SaveToStr():string;
var
  Json:TQJson;
  MemStream:TMemoryStream;
begin
  Result:=EmptyStr;  
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create; 
  try
    Json.Add('ModuleKey').AsString:=TSmOrderMstModel.cModuleKey;
    Json.Add(TSmOrderMstModel.cObjNameKey).AsString:=Self.ClassName;
    
    fCdsModel.SaveToStream(MemStream);
    Json.Add(TSmOrderMstModel.cCdsModelKey).AsString:=string(TSK0001Comm.MemStreamToBase64Str(MemStream));
    
    Json.Add(TSmOrderMstModel.cObjGUIDKey).AsString:=fObjGUID;
    Json.Add('OrderId').AsString:=fOrderId;
    Json.Add('OrderBillType').AsInteger:=fOrderBillType;
    Json.Add('CustomerId').AsString:=fCustomerId;
    Json.Add('OrderStatus').AsInteger:=fOrderStatus;
    Json.Add('OrderRemark').AsString:=fOrderRemark;
    Json.Add('InvoiceTypeId').AsString:=fInvoiceTypeId;
    Json.Add('PaywayId').AsString:=fPaywayId;
    Json.Add('ShipTypeId').AsString:=fShipTypeId;
    Json.Add('OrgId').AsString:=fOrgId;
    Json.Add('EmpId').AsString:=fEmpId;
    Json.Add('DeptId').AsString:=fDeptId;
    Json.Add('LinkmanId').AsString:=fLinkmanId;
    Json.Add('OrderAmount').AsFloat:=fOrderAmount;
    Json.Add('OrderSendStatus').AsInteger:=fOrderSendStatus;
    Json.Add('OrderSettlementStatus').AsInteger:=fOrderSettlementStatus;
    Json.Add('RecordPsn').AsString:=fRecordPsn;
    Json.Add('RecordDate').AsDateTime:=fRecordDate;
    Json.Add('ModifyPsn').AsString:=fModifyPsn;
    Json.Add('ModifyDate').AsDateTime:=fModifyDate;
    Json.Add('ApprovePsn').AsString:=fApprovePsn;
    Json.Add('ApproveDate').AsDateTime:=fApproveDate;
    Json.Add('CancelPsn').AsString:=fCancelPsn;
    Json.Add('CancelDate').AsDateTime:=fCancelDate;
    Json.Add('TimeStamp').AsInt64:=fTimeStamp;
    Result:=Json.AsString;
  finally
    Json.Free;
    MemStream.Free;
  end;
end;

constructor TSmOrderMstList.Create();
begin
  inherited Create;
  fCdsModel:=TClientDataSet.Create(nil);
  fSortKeys:=TStringList.Create;
  ResetObject;
end;

destructor TSmOrderMstList.Destroy;
begin
  FreeAndNil(fCdsModel);
  Clear;
  FreeAndNil(fSortKeys);
  inherited Destroy;
end;

procedure TSmOrderMstList.ResetObject;
begin
  Clear;
end;

function TSmOrderMstList.LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
var
  Item:TSmOrderMst;
begin
  ResetObject;
  if iCds<>nil then
  begin
    iCds.First;
    while not iCds.Eof do
    begin
      Item:=TSmOrderMst.Create;
      if Item.LoadFromCDS(iCds,oError) then
      begin
        if Add(Item)=EmptyStr then
        begin
          Item.Free;
          Result:=False;
          Exit;
        end;
      end else begin
        Result:=False;
        Exit;
      end;
      iCds.Next;
    end;
  end else begin
    oError:=Format(string('调用%s.LoadFromCDS函数参数错误! 错误内容传入的数据集为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSmOrderMstList.SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
var
  Key:string;
begin
  if (iCds<>nil) then
  begin
  for Key in fSortKeys do
  begin
    iCds.Append;
    if Items[Key].ObjectToCDS(iCds,oError) then
    begin
      iCds.Post;
    end else begin
      Result:=False;
      Exit;
    end;
  end;
  end else begin
    oError:=Format(string('调用%s.SaveToCDS函数参数错误! 错误内容:传入的数据集为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSmOrderMstList.LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
var
  Json,ItemJson,ArrayJson:TQJson;
  Item:TSmOrderMst;
  MemStream:TMemoryStream;
begin
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create;
  try
    if Json.TryParse(iJsonStr) then
    begin
      if (Json.ItemByName(TSmOrderMstModel.cObjNameKey)<>nil) then
      begin
        if (Self.ClassName<>Json.ItemByName(TSmOrderMstModel.cObjNameKey).AsString) then
        begin
          //oError:=Format(string('调用%s.LoadFromStr函数参数错误! 错误内容:对象类名不匹配.'),[Self.ClassName]);
          //Result:=False;
          //Exit;
        end;
      end else begin
        oError:=Format(string('调用%s.LoadFromStr函数参数错误! 错误内容:缺少%s属性节点.'),[Self.ClassName,TSmOrderMstModel.cObjNameKey]);
        Result:=False;
        Exit;
      end;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数参数错误! 错误内容:解析Json失败.iJsonStr:%s'),[Self.ClassName,iJsonStr]);
      Result:=False;
      Exit;
    end;
    if (Json.ItemByName(TSmOrderMstModel.cObjItemsKey)<>nil) and
      (Json.ItemByName(TSmOrderMstModel.cObjItemsKey).IsArray) then
    begin
      ResetObject;
      if Json.ItemByName(TSmOrderMstModel.cCdsModelKey)<>nil then
      begin
        TSK0001Comm.Base64StrToMemStream(AnsiString(Json.ItemByName(TSmOrderMstModel.cCdsModelKey).AsString),MemStream);
        if MemStream.Size>0 then
        begin
          fCdsModel.LoadFromStream(MemStream);
        end;
      end;
      ArrayJson:=Json.ItemByName(TSmOrderMstModel.cObjItemsKey);
      for ItemJson in ArrayJson do
      begin
        Item:=TSmOrderMst.Create;
        Item.LoadFromStr(ItemJson.AsString,oError);
        if Add(Item)=EmptyStr then
        begin
          Item.Free;
          Result:=False;
          Exit;
        end;
      end;
    end else begin
      Result:=False;
      Exit;
    end;
  finally
    Json.Free;
    MemStream.Free;
  end;
  Result:=True;
end;

function TSmOrderMstList.SaveToStr():string;
var
  Key:string;
  Json,ItemJson,ArrayJson:TQJson;
  MemStream:TMemoryStream;
begin
  Result:=EmptyStr;
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create;
  try
    Json.Add(TSmOrderMstModel.cObjNameKey).AsString:=Self.ClassName;
    fCdsModel.SaveToStream(MemStream);
    Json.Add(TSmOrderMstModel.cCdsModelKey).AsString:=string(TSK0001Comm.MemStreamToBase64Str(MemStream));

    ArrayJson:=Json.Add(TSmOrderMstModel.cObjItemsKey,jdtArray);
    for Key in fSortKeys do
    begin
      ItemJson:=TQJson.Create;
      if ItemJson.TryParse(Items[Key].SaveToStr()) then
      begin
        ArrayJson.Add(ItemJson);
      end else begin
        ItemJson.Free;
      end;
    end;
    Result:=Json.AsString;
  finally
    Json.Free;
    MemStream.Free;
  end;
end;

function TSmOrderMstList.FindByKey(const iKey:string):TSmOrderMst;
var
  Item:TSmOrderMst;
begin
  if TryGetValue(iKey,Item) then
  begin
    Result:=Item;
  end else begin
    Result:=nil;
  end;
end;

function TSmOrderMstList.IsExist(iKey:string):Boolean;
var
  Item:TSmOrderMst;
begin
  Result:= TryGetValue(iKey,Item);
end;

function TSmOrderMstList.Add(const iItem:TSmOrderMst):string;
begin
  if iItem.ObjGUID<>EmptyStr then
  begin
    inherited Add(iItem.ObjGUID,iItem);
    Result:=iItem.ObjGUID;
    fSortKeys.Add(iItem.ObjGUID);
  end else begin
    Result:=EmptyStr;
  end;
end;

function TSmOrderMstList.Delete(const iKey:string):Boolean;
var
  Ops:Integer;
begin
  if ContainsKey(iKey) then
  begin
    Remove(iKey).Free;
    Ops:=fSortKeys.IndexOf(iKey);
    if Ops<>-1 then
    begin
      fSortKeys.Delete(Ops);
    end;
    Result:=True;
  end else begin
    Result:=False;
  end;
end;

function TSmOrderMstList.Remove(const iKey:string):TSmOrderMst;
var
  Ops:Integer;
begin
  if ContainsKey(iKey) then
  begin
    Result:=Items[iKey];
    Ops:=fSortKeys.IndexOf(iKey);
    if Ops<>-1 then
    begin
      fSortKeys.Delete(Ops);
    end;
    inherited Remove(iKey);
  end else begin
    Result:=nil;
  end;
end;

procedure TSmOrderMstList.Clear;
var
  Key:string;
begin
  for Key in Keys do
  begin
    if Items[Key]<>nil then
    begin
      Items[Key].Free;
    end;
  end;
  fSortKeys.Clear;
  inherited Clear;
end;

end.
